﻿$(document).ready(
    function() {
        Sys.WebForms.PageRequestManager.getInstance().add_endRequest(EndRequestHandler);
        startUp();
        //Info pop up, add 'Help' to title.
        //$(".infoPopUp").click(function(event) { event.preventDefault(); });
        //$('.infoPopUp').cluetip({ showTitle: true, attribute: 'rel', titleAttribute: 'title', tracking: true, splitTitle: '*', hoverIntent: { sensitivity: 3, interval: 200, timeout: 300 }
        //});
    });

    var startUp = function() {
        bubbleupValidation();
        alternateForm();
        //set info pupup options
        $('.infoPopUp').cluetip({ splitTitle: '|', showTitle: true, attribute: 'rel', titleAttribute: 'title', tracking: true, splitTitle: '*', hoverIntent: { sensitivity: 3, interval: 200, timeout: 500 }
        });

        //remove default behaviour for info pop up
        $(".infoPopUp").click(
            function(event) { event.preventDefault(); }
        );

        //expander elements
        $(".expander").click(function() {
            $(".panel-details, .expander").toggle();
        });

        //Set slide toggle for stats bar
        $(".visibleArea h3").click(
            function() {

                $("div#headerContent ul li ul").slideToggle("slow");
            }
        );

        //set slide toggle for home page content
        $(".slideVisible").click(
            function() {
                $(this).siblings().slideToggle("slow");

            }
        );

        $(".ameeSubQuestion").each(function(i) {
            $(this).children('.formItem:last').css({ borderWidth: '0' });
        });


        // Home Page Stats Carousel
        //this has been turned off untill the second set of stats is needed.
        //$(".hpCarousel").jCarouselLite({ visible: 1, auto: 8500, speed: 250, vertical: false });
    }

    var alternateForm = function() {
        var tmp = $("div.form .alternate .formItem:visible");
        tmp.each(function(index, item) {
            var even = index % 2 == 0;
            if (even) {
                $(item).addClass("even");
                $(item).removeClass("odd");
            }
            else {
                $(item).addClass("odd");
                $(item).removeClass("even");
            }
        });
    }

    var bubbleupValidation = function() {
        //remove all existing bubbled up errors
        $(".formItem.validationError").removeClass("validationError");
        //get validators and if not validated bubble up error to list item
        if (typeof (Page_Validators) == "object") {
            $(Page_Validators).each(
                function() {
            if (!this.isvalid || this.style.display == "inline" || this.style.display == "block") {
                        $(this).parent(".formItem").addClass("validationError");
                    }
                }
            );
        }
    }

    function EndRequestHandler(sender, args) {
        if (args.get_error() == undefined) {
            startUp();
        }
    }
